'''
author:
desc:
date:2019/11/29 9:57
1分析页面元素
'''
from lib.pyse import Pyse
import datetime
import time


class BasePage(object):

	def __init__(self):
		# Pyse() 实例化-启动浏览器
		self.pyse = Pyse()

	@property
	def driver(self):
		return self.pyse.driver

	def open(self):
		self.pyse.open('http://10.17.4.33:8081/zentao/user-login.html')

	def element_wait(self, css):
		self.pyse.element_wait(css)

	def quit(self):
		self.pyse.quit()


# 相关的逻辑都在page中
class Login(BasePage):

	def set_username(self):
		css = 'css=>#account'
		value = 'shaotingting'
		self.pyse.type(css, value)

	def set_password(self):
		css = 'css=>input[name="password"]'
		value = 'Stt123456'
		self.pyse.type(css,value)

	def click_login(self):
		css = 'css=>#submit'
		self.pyse.click(css)

	def forget_password(self):
		css = 'css=>#verifyRand'
		self.pyse.click(css)

	def check_login(self, name):
		# 如果登陆成功则会有退出按钮,调用wait_and_save_exception
		css = 'css=>a[href="/zentao/user-lgout.html"]'
		# 返回true or false
		return self.pyse.wait_and_save_exception(css, name)


class BugList(Login):
	def test_button(self):
		time.sleep(10)
		css = "xpath=>//*[@id='navbar']/ul/li[4]/a"
		self.pyse.click(css)

	def bug_button(self):
		css ='css=>a[href="/zentao/bug-browse-1.html"]'
		self.pyse.click(css)

	def sumbit_bug(self):
		css = 'css=>a[href^="/zentao/bug-create"]'
		self.pyse.click(css)

	def batch_sumbit_bug(self):
		css = 'css=>a[href^="/zentao/bug-batchCreate"]'
		self.pyse.click(css)


class NewBug(BugList):
	def produce(self):
		# 选择下拉框信息，由于select display不可见，需要修改为可见后选择信息
		js ="document.getElementById('product').style.display='';"
		css = 'css=>#product'
		self.pyse.js(js)
		self.pyse.select_by_value(css, "1")

	def module(self):
		# 方法一
		# js = "document.getElementById('module').style.display='';document.getElementById('module').style.width='10px'"
		# self.pyse.js(js)
		# css = 'css=>#module'
		# time.sleep(5)
		# self.pyse.select_by_value(css, "1")
		# 方法二
		css = 'css=>#module_chosen>a'
		self.pyse.click(css)
		css = 'css=>#module_chosen>div>ul>li[data-option-array-index="1"]'
		self.pyse.click(css)

	def chosen_project(self):
		css = 'css=>#project_chosen>a'
		self.pyse.click(css)
		css = 'css=>#project_chosen>div>ul>li[title="云物业"]'
		self.pyse.click(css)

	def version(self):
		# js = "document.getElementById('openedBuild').style.display='';"
		# self.pyse.js(js)
		# css = 'css=>#openedBuild'
		# self.pyse.select_by_value(css, "trunk")
		css = 'css=>#openedBuild_chosen'
		self.pyse.click(css)
		# css ='xpath=>//div[@id="openedBuild_chosen"/div/ul/li[@data-option-array-index="0"]'
		css = 'css=>#openedBuild_chosen>div>ul>li[data-option-array-index="0"]'
		self.pyse.click(css)

	def assign(self):
		# js = "document.getElementById('assignedTo').style.display='';"
		# self.pyse.js(js)
		# css = 'css=>#assignedTo'
		# self.pyse.select_by_value(css, "admin")
		css = 'css=>#assignedTo_chosen>a'
		self.pyse.click(css)
		css = 'css=>#assignedTo_chosen>div>ul>li[title="A:admin"]'
		self.pyse.click(css)

	def deadline(self):
		# 日期类型input type="text"
		css = 'css=>#deadline'
		#datetime.date.today()
		self.pyse.type(css,'2019-12-02')

	def type(self):
		# js = "document.getElementById('type').style.display='';"
		# self.pyse.js(js)
		# css = 'css=>#type'
		# self.pyse.select_by_value(css, "codeerror")
		css = 'css=>#type_chosen>a'
		self.pyse.click(css)
		css = 'css=>#type_chosen>div>ul>li[title="代码错误"]'
		self.pyse.click(css)

	def os(self):
		# js = "document.getElementById('os').style.display='';"
		# self.pyse.js(js)
		# css = 'css=>#os'
		# self.pyse.select_by_value(css, "win10")
		css = 'css=>#os_chosen>a'
		self.pyse.click(css)
		css = 'css=>#os_chosen>div>ul>li[title="Windows 10"]'
		self.pyse.click(css)

	def browser(self):
		js = "document.getElementById('browser').style.display='';"
		self.pyse.js(js)
		css = 'css=>#browser'
		self.pyse.select_by_value(css, "ie11")
		css = 'css=>#browser_chosen>a'
		self.pyse.click(css)
		css = 'css=>#browser_chosen>div>ul>li[title="IE10"]'
		self.pyse.click(css)

	def title(self):
		css ='css=>#title'
		self.pyse.type(css,'功能bug')

	def step(self):
		# css = 'css=>.ke-edit-iframe'
		# self.pyse.switch_to_frame(css)
		# css = 'css=>body'
		# # self.pyse.clear(css)
		# self.pyse.type(css,'预期结果为空')
		# self.pyse.switch_to_parent_frame()
		js = "document.getElementById('steps').style.display='block';"
		self.pyse.js(js)
		css = 'css=>#steps'
		self.pyse.type(css, '<p>预期结果为空</p>')

	def save(self):
		# js = 'window.scrollTo（0,1000)'
		# self.pyse.js(js)
		css = 'css=>#submit'
		self.pyse.click(css)

	def check_newbug(self, name):
		css = 'css=>a[href^="/zentao/bug-create"]'
		# 返回true false
		return self.pyse.wait_and_save_exception(css, name)

	def check_assgin(self):
		css = 'css=>tr[data-id="2"]'
		self.pyse.element_wait(css)
		element = self.pyse.get_element(css)
		# 元素下还有子元素，可用连续使用
		tds = element.find_elements_by_tag_name('td')
		text = tds[8].get_attribute('innerText')
		# 如果出错就截图
		# self.pyse.wait_and_save_exception(css, name)
		return text

class Page(NewBug):
	pass
if __name__ =='__main__':
	page = Page()
	page.open()
	page.set_username()
	page.set_password()
	page.click_login()
	page.test_button()
	page.bug_button()
	# page.sumbit_bug()
	# page.module()
	# page.chosen_project()
	# page.version()
	# # time.sleep(5)
	# page.assign()
	# time.sleep(5)
	# page.deadline()
	# page.type()
	# page.os()
	# page.browser()
	# page.title()
	# time.sleep(5)
	# page.step()
	# time.sleep(5)
	# page.save()
	page.check_assgin('89')
